package edu.cmu.javadb.plan;

import edu.cmu.javadb.plan.visitor.Visitor;

public abstract class PlanNode implements Cloneable {

	/*
	 * The name of the node, if this is not a table or alias, it will get a random generated name
	 */
	protected String name;
	
	public PlanNode() {
		name = IdGenerator.generatorId();
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}
	
	private PlanNode father = null;
	
	public PlanNode getRoot() {
		PlanNode res = this;
		while (res.getFather() != null) {
			res = res.getFather();
		}
		return res;
	}
	
	public PlanNode getFather() {
		return father;
	}

	public void setFather(PlanNode father) {
		this.father = father;
	}
		
    public PlanNode clone() throws CloneNotSupportedException {
    	PlanNode res = (PlanNode) super.clone();
    	res.name = IdGenerator.generatorId();
    	return res;
    }
	
	abstract public <T> T accept(Visitor<T> visitor);
	
	abstract public <T> void visitChildren(Visitor<T> visitor);
	
	abstract public boolean containsRelation(String name);
}
